﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;
using JM.JCY.Common.Utilities;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent.IncorruptArchives.LingDaoSalaryDetails
{
    public partial class LingDaoSalaryDetailsUserControl : UserControl
    {
        public int _caseId
        {
            set { ViewState["_caseId"] = value; }
            get { return (int)ViewState["_caseId"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (IncorruptProxy proxy = new IncorruptProxy())
                {
                    if (Request.QueryString["CaseId"] != null)
                    {
                        _caseId = Int32.Parse(Request.QueryString["CaseId"].ToString());

                        // load data
                        var loadDT = proxy.GetLingDaoSalary(_caseId, string.Empty);
                        LoadForEdit(loadDT);
                        LoadForView(loadDT);
                        Session["EditLingDaoSalaryDT"] = loadDT;
                    }

                }

            }

            // switch controls
            if (this.hfIsEdit.Value != "1")
                SwitchControls(true, false);
            else
                SwitchControls(false, true);
        }

        private void LoadForView(IncorruptDataSet.T_LingDaoSalaryDataTable loadDT)
        {
            var loadRow = loadDT[0];

            this.lblName.Text = loadRow.Name;
            this.lblGender.Text = loadRow.Gender;
            this.lblPoliticSituation.Text = loadRow.PoliticSituation;
            this.lblPosition.Text = loadRow.Position;
            this.lblYear.Text = loadRow.Year;
            this.lblSalary.Text = loadRow.Salary;
            this.lblBenefits.Text = loadRow.Benefits;
            this.lblWorkCharge.Text = loadRow.WorkCharge;
            this.lblOthers.Text = loadRow.Others;
            this.lblTotal.Text = loadRow.Total;
            this.lblInstruction.Text = loadRow.Instruction;
            this.lblRemark.Text = loadRow.Remark;
            this.lblFillDate.Text = loadRow.FillDate.ToString(JCYConstant.Date_Format_CN);
        }

        private void LoadForEdit(IncorruptDataSet.T_LingDaoSalaryDataTable loadDT)
        {
            var loadRow = loadDT[0];

            this.txtName.Text = loadRow.Name;
            this.rbGender.SelectedValue = loadRow.Gender;
            this.txtPoliticSituation.Text = loadRow.PoliticSituation;
            this.txtPosition.Text = loadRow.Position;
            this.txtYear.Text = loadRow.Year;
            this.txtSalary.Text = loadRow.Salary;
            this.txtBenefits.Text = loadRow.Benefits;
            this.txtWorkCharge.Text = loadRow.WorkCharge;
            this.txtOthers.Text = loadRow.Others;
            this.txtTotal.Text = loadRow.Total;
            this.txtInstruction.Text = loadRow.Instruction;
            this.txtRemark.Text = loadRow.Remark;
            this.hfFillDate.Value = loadRow.FillDate.ToString();
        }

        private void SwitchControls(bool isLabelsShow, bool isControlsShow)
        {
            //Lables
            this.lblName.Visible = isLabelsShow;
            this.lblGender.Visible = isLabelsShow;
            this.lblPoliticSituation.Visible = isLabelsShow;
            this.lblPosition.Visible = isLabelsShow;
            this.lblYear.Visible = isLabelsShow;
            this.lblSalary.Visible = isLabelsShow;
            this.lblBenefits.Visible = isLabelsShow;
            this.lblWorkCharge.Visible = isLabelsShow;
            this.lblOthers.Visible = isLabelsShow;
            this.lblTotal.Visible = isLabelsShow;
            this.lblInstruction.Visible = isLabelsShow;
            this.lblRemark.Visible = isLabelsShow;
            this.lblFillDate.Visible = isLabelsShow;

            //Controls
            this.txtName.Visible = isControlsShow;
            this.rbGender.Visible = isControlsShow;
            this.txtPoliticSituation.Visible = isControlsShow;
            this.txtPosition.Visible = isControlsShow;
            this.txtYear.Visible = isControlsShow;
            this.txtSalary.Visible = isControlsShow;
            this.txtBenefits.Visible = isControlsShow;
            this.txtWorkCharge.Visible = isControlsShow;
            this.txtOthers.Visible = isControlsShow;
            this.txtTotal.Visible = isControlsShow;
            this.txtInstruction.Visible = isControlsShow;
            this.txtRemark.Visible = isControlsShow;
            this.dtFillDate.Visible = isControlsShow;
            this.hr4.Visible = isControlsShow;
            this.tbSubmit.Visible = isControlsShow;
        }

        protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
        {
            using (IncorruptProxy proxy = new IncorruptProxy())
            {
                if (Session["EditLingDaoSalaryDT"] != null)
                {
                    var editDT = (IncorruptDataSet.T_LingDaoSalaryDataTable)Session["EditLingDaoSalaryDT"];
                    var editRow = editDT[0];

                    editRow.Name = this.txtName.Text;
                    editRow.Gender = this.rbGender.SelectedValue;
                    editRow.PoliticSituation = this.txtPoliticSituation.Text;
                    editRow.Position = this.txtPosition.Text;
                    editRow.Year = this.txtYear.Text;
                    editRow.Salary = this.txtSalary.Text;
                    editRow.Benefits = this.txtBenefits.Text;
                    editRow.WorkCharge = this.txtWorkCharge.Text;
                    editRow.Others = this.txtOthers.Text;
                    editRow.Total = this.txtTotal.Text;
                    editRow.Instruction = this.txtInstruction.Text;
                    editRow.Remark = this.txtRemark.Text;
                    editRow.FillDate = this.dtFillDate.SelectedDate;

                    JCYUtility.UpdateCommonFields(editRow, "Iori");
                    proxy.UpdateLingDaoSalary(editDT);

                    Response.Redirect("LingDaoSalary.aspx");
                }
            }

        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("LingDaoSalary.aspx");
        }

        protected void lkbEditLingDaoSalary_Click(object sender, EventArgs e)
        {
            this.hfIsEdit.Value = "1";
            SwitchControls(false, true);
            this.dtFillDate.SelectedDate = Convert.ToDateTime(this.hfFillDate.Value);
        }
    }
}
